package com.chaoyer.cynet.common.utils;

import java.util.List;
import java.util.Random;


/**
 * 随机 工具类
 */
public class RandomUtils {

    private static Random random = new Random(System.currentTimeMillis());

    //获取一段随机数 length表示生成字符串的长度
    public static String getRandomString(int length) {
        String base = "0123456789";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    // list中随机一个
    public static <T> T choice(List<T> list) {
        if (list == null || list.size() == 0) {
            return null;
        }
        return list.get(random.nextInt(list.size()));
    }

}